<?php

// vim: expandtab shiftwidth=2 softtabstop=2 tabstop=2

/**
 * Group of functions to operations on arrays
 *
 * PHP version 5
 *
 * @category  Namespaces
 * @package   mPack
 * @author    Marcin Sztolcman <marcin /at/ urzenia /dot/ net>
 * @copyright 2006 Marcin Sztolcman
 * @license   GPL v.2
 * @version   SVN: $Id: ns_arrays.php 29 2008-01-20 23:15:20Z urzenia $
 * @link      http://m-pack.googlecode.com
 */

/**
 * Group of functions to operations on arrays
 *
 * @category  Namespaces
 * @package   mPack
 * @author    Marcin Sztolcman <marcin /at/ urzenia /dot/ net>
 * @copyright 2006 Marcin Sztolcman
 * @license   GPL v.2
 * @version   SVN: $Id: ns_arrays.php 29 2008-01-20 23:15:20Z urzenia $
 * @link      http://m-pack.googlecode.com
 */
abstract class Arrays {

  /**
   * Will flat an multilevel array
   *
   * Keys are not preserved, and dupliacted values are stored.
   *
   * @param array $array
   *
   * @return array flatten array
   * @access public
   * @static
   */
  static public function flat($array)
  {
    $arr = array();
    foreach ($array as $a) {
      if (is_array($a)) {
        $arr = array_merge($arr, array_values(self::flat($a)));
      } else {
        $arr[] = $a;
      }
    }
    return $arr;
  }

  /**
   * Prints array content
   *
   * @param array   $array
   * @param boolean $exit if true, call exit after display array content
   * @param string  $funcName function used to display array content
   *
   * @return void
   * @static
   */
  public static function debug($array, $exit=false, $funcName='print_r')
  {
    echo '<pre>';
    if (!function_exists($funcName)) {
      $funcName = 'print_r';
    }
    $funcName($array);
    echo '</pre>';
    if ($exit) {
      exit;
    }
  }

  /**
   * Check for keys in two arrays are the same.
   *
   * @param array $arr1
   * @param array $arr2
   *
   * @return boolean
   * @static
   */
  public static function checkKeys1($arr1, $arr2)
  {
    return (array_keys($arr1) == array_keys($arr2));
  }

  /**
   * Check for keys in array $arr are the same as array $key
   *
   * @param array $keys
   * @param array $arr
   *
   * @return boolean
   * @static
   */
  public static function checkKeys2($keys, $arr)
  {
    return ($keys == array_keys($arr2));
  }

  /**
   * Joins multidimensional arrays with some glue
   *
   * @param string $glue
   * @param array $array
   *
   * @return string
   * @static
   */
  public static function multijoin($glue, $array)
  {
    return join($glue, Arrays::flat($array));
  }

}

?>
